package cc.yihy.storage.aliyun.bean;

import cc.yihy.storage.StorageClient;
import cc.yihy.storage.aliyun.AliYunProperties;
import cc.yihy.storage.entity.UpResult;
import cc.yihy.storage.util.StorageUtil;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Yihy
 * @version V1.0
 * @Package cc.yihy.storage
 * @Description:
 * @date 2017/6/27 15:50
 */
public class SimpleAliYunOSSClient implements StorageClient {

    private final static Logger log = LoggerFactory.getLogger(SimpleAliYunOSSClient.class);


    private AliYunProperties aliYunProperties;

    public SimpleAliYunOSSClient(AliYunProperties aliYunProperties) {
        this.aliYunProperties = aliYunProperties;
    }

    @Override
    public UpResult upload(String filePath, String fileKey) throws IOException {


        UpResult upResult = new UpResult();
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(aliYunProperties.getEndpoint(), aliYunProperties.getAccessKeyId(), aliYunProperties.getAccessKeySecret());
        // 上传文件
        PutObjectResult result = ossClient.putObject(aliYunProperties.getBucketName(), fileKey, new File(filePath));


        upResult.setSuccess(StringUtils.isNotBlank(result.getETag()));
        upResult.setFileName(fileKey);
        // 关闭client
        ossClient.shutdown();

        return upResult;
    }

    @Override
    public UpResult upload(InputStream inputStream, String fileKey) {
        UpResult upResult = new UpResult();
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(aliYunProperties.getEndpoint(), aliYunProperties.getAccessKeyId(), aliYunProperties.getAccessKeySecret());
        // 上传文件流

        PutObjectResult result = ossClient.putObject(aliYunProperties.getBucketName(), fileKey, inputStream);

        upResult.setSuccess(StringUtils.isNotBlank(result.getETag()));
        upResult.setFileName(fileKey);
        // 关闭client
        ossClient.shutdown();
        try {
            inputStream.close();
        } catch (IOException e) {
            log.info("io error", e);
            e.printStackTrace();
        }
        return upResult;
    }

    @Override
    public String getDomain() {
        return aliYunProperties.getDomain();
    }

    @Override
    public String getImageUrl(String fileKey) {

        return StorageUtil.filePath(aliYunProperties.getDomain(), fileKey);
    }

    @Override
    public boolean delete(String fileKey) {

        try {
            // 创建OSSClient实例
            OSSClient ossClient = new OSSClient(aliYunProperties.getEndpoint(), aliYunProperties.getAccessKeyId(), aliYunProperties.getAccessKeySecret());
            // 删除Object
            ossClient.deleteObject(aliYunProperties.getBucketName(), fileKey);
            // 关闭client
            ossClient.shutdown();
            return true;
        } catch (OSSException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return false;
    }
}
